package day01;

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
//import java.util.List;
import java.util.Scanner;
import java.util.Random;

/**
 * 记单词程序
 */
public class StudyEnglish {
    public static void Allword() throws IOException {
        boolean quit = true;
        do {


            Scanner scanner = new Scanner(System.in);
            System.out.print("请选择\n直接进入（Enter）\n输入新单词(input)\n退出请按输入(exit)：");
            String user = scanner.nextLine();
            if ("exit".equalsIgnoreCase(user)) {
                System.out.println("欢迎下次再来！！！");
                quit = false;
            } else if ("Enter".equalsIgnoreCase(user)) { //忽略大小写对比
//                File fileE = new File("./study/test/E_8_30.txt"); //每日单词
//                File fileC = new File("./study/test/C_8_30.txt"); //每日释义
            File fileE = new File("./study/English.txt"); //总单词
            File fileC = new File("./study/Chinese.txt"); //总释义

                FileInputStream InEnglish = new FileInputStream(fileE); //链接单词
                FileInputStream InChinese = new FileInputStream(fileC); //链接中文释义
                /**
                 * 随机输出一个单词
                 */
                long lenE = fileE.length();
                byte[] subsE = new byte[(int) lenE];
                InEnglish.read(subsE);
                /**
                 * 对应释义
                 */
                long lenC = fileC.length();
                byte[] subsC = new byte[(int) lenC];
                InChinese.read(subsC);

                String lineE = new String(subsE, StandardCharsets.UTF_8); //英语转字符串
                String lineC = new String(subsC, StandardCharsets.UTF_8); //释义转字符串

                String[] arrE = lineE.split(",");//分隔字符串
                String[] arrC = lineC.split("，");//分隔字符串

                Random random = new Random();

                while (true) {
                    int index = random.nextInt((arrE.length - 1));
                    System.out.println((index + 1) + " 单词：" + arrE[index]);
                    System.out.print("请输入中文释义：");
                    String userChinese = scanner.nextLine();
                    if (userChinese.equals("q")) {
                        System.out.println("已退出");
                        quit = false;

                        break; //如果输入q，就直接退出
                    }

                    if (arrC[index].contains(userChinese)) { //如果用户输入的内容，是对应的：包含算法
//                    if (userChinese.contains(arrC[index])) { //如果用户输入的内容，是对应的：包含算法
                        System.out.println("输入正确！     " + arrC[index]);

                    } else {
                        System.out.print("错误！");
                        System.out.println("释义是：" + arrC[index]);
                    }
                }
                //退出记单词状态时，需把流关闭
                InEnglish.close();
                InChinese.close();
                //如果选择手动输入新单词
            } else if ("input".equalsIgnoreCase(user)) {
//            File fileE1 = new File("./study/English.txt");
//            File fileC1 = new File("./study/English.txt");

              FileOutputStream OutEnglish = new FileOutputStream("./study/English.txt", true);
              FileOutputStream OutChinese = new FileOutputStream("./study/Chinese.txt", true);
//              FileOutputStream OutEnglish = new FileOutputStream("./study/test/E_8_30.txt", true);
//              FileOutputStream OutChinese = new FileOutputStream("./study/test/C_8_30.txt", true);
                Scanner scanner1 = new Scanner(System.in);
                //编辑状态
                while (true) {
                    System.out.print("请输入单词：");
                    String word1 = scanner1.nextLine();
                    String zoneE = "[a-zA-Z0-9_]+"; //正则表达式判断----------- zone：区域
                    boolean matchE = word1.matches(zoneE);//如果满足字母判断
                    if (!matchE) {
                        System.out.println("请不要输入奇奇怪怪的东西，如：汉字等特殊符号");
                        continue;
                    }
                    if ("q".equals(word1)) {
                            System.out.println("已退出编辑状态！");
                            break;
                    }else if(matchE){
                            OutEnglish.write(word1.getBytes(StandardCharsets.UTF_8));
                            OutEnglish.write(",".getBytes(StandardCharsets.UTF_8));
                            System.out.println("已存入单词数据库！");
                    }
                    /**
                     * 开始输入中文
                     */
                    while (true) {
                        System.out.print("请输入" + word1 + "的中文释义：");
                        String word2 = scanner1.nextLine();
                        String zoneC = "[a-zA-Z0-9_]+"; //正则表达式判断----------- zone：区域
                        boolean matchC = word2.matches(zoneC);
                        if (!matchC) {
                            OutChinese.write(word2.getBytes(StandardCharsets.UTF_8));
                            OutChinese.write("，".getBytes(StandardCharsets.UTF_8));
                            System.out.println("已存入中文释义数据库！");

                            break;
                        } else {
                            System.out.println("请不要输入奇奇怪怪的例如：字符字母数字！！！");
                            continue;
                        }
                    }
                }
                OutEnglish.close();  //退出写入状态时关闭输入流
                OutChinese.close(); //退出写入状态时关闭输入流
            }
        } while (quit);

    }

    public static void JF() {
        /**
         * 创建窗口及面板
         */
        JFrame win = new JFrame();
        JPanel panel = new JPanel(null);
        win.setSize(650, 230);
        /**
         *创建文本框
         */
        JTextArea sourceE = new JTextArea();
        JTextArea sourceC = new JTextArea();
        /**
         *文本框位置
         */
        sourceE.setLocation(10, 70);
        sourceC.setLocation(320, 70);
        /**
         *文本框大小
         */
        sourceE.setSize(300, 100);
        sourceC.setSize(300, 100);
        /**
         *设置换行
         */
        sourceE.setLineWrap(true);
        sourceC.setLineWrap(true);
        /**
         *按钮:进入，输入，退出，保存
         */
        JButton buttonEnter = new JButton("Enter");
        JButton buttonInput = new JButton("Input");
        JButton buttonExit = new JButton("Exit");
        JButton buttonSave = new JButton("Save");
        /**
         *按钮参数：位置，尺寸
         */
        //位置
        buttonEnter.setLocation(50, 10);
        buttonInput.setLocation(200, 10);
        buttonExit.setLocation(350, 10);
        buttonSave.setLocation(500, 10);
        //大小
        buttonEnter.setSize(90, 50);
        buttonInput.setSize(90, 50);
        buttonExit.setSize(90, 50);
        buttonSave.setSize(90, 50);
        //按钮字体---------------------------------------------------btn1.setPreferredSize( new Dimension(120,60));

        Font font = new Font("黑体", Font.BOLD, 20);//设置黑体  粗体
        buttonEnter.setFont(font);  //设置新字体
        buttonInput.setFont(font);  //设置新字体
        buttonExit.setFont(font);  //设置新字体
        buttonSave.setFont(font);  //设置新字体
//      按钮监听
        buttonEnter.addActionListener(e -> {
            System.out.println("Enter");
        });
        buttonInput.addActionListener(e -> {
            System.out.println("Imput");
        });
        buttonExit.addActionListener(e -> {
            System.out.println("exit");
        });
        buttonSave.addActionListener(e -> {
            System.out.println("save");
        });
        /**
         * 面板添加组件
         */
        //添加按钮
        panel.add(buttonEnter);
        panel.add(buttonInput);
        panel.add(buttonExit);
        panel.add(buttonSave);
        //添加文本框
        panel.add(sourceE);
        panel.add(sourceC);
        //窗口添加面板
        win.add(panel);
        win.setLocationRelativeTo(null);//窗口居中
        win.setVisible(true);//显示窗口
        win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//退出时关闭程序
    }

    public static void main(String[] args) throws IOException {
//        JF();
        Allword();
    }
}
